#include "process.h"
#include <string.h>
#include <unistd.h>

#define NUM 100
#define CHAR '#'

//version2
void Flushprocess(double totle, double cur)
{
    //step1：构建提示符
    const char* str = "|/-\\";
    int str_len = strlen(str);
    static int len = 0;
    double rate = (cur / totle) * 100.0;//计算出当前下载了多少

    //step2：构建缓冲区
    char buf[NUM];
    memset(buf, '\0', sizeof(buf));

    //step3：输出进度条
    int count = (int)rate;
    int i = 0;
    for (;i < count; ++i) buf[i] = CHAR;
    printf("[%-100s][%.1lf%%][%c]\r", buf, rate, str[len % str_len]);
    ++len;
    //刷新缓冲区
    fflush(stdout);
}

//version1
//void Process()
//{
//    //旋转字符
//    const char* str = "|/-\\";
//    int str_len = strlen(str);
//    //创建缓冲区
//    char buf[NUM];
//    memset(buf, '\0', sizeof(buf));
//
//    int count = 0;
//    while (count <= NUM)
//    {
//        printf("[%-100s][%d%%][%c]\r", buf, count, str[count % str_len]);
//        usleep(100000);
//        buf[count] = '#';
//        fflush(stdout);
//        ++count;
//    }
//    printf("\n");
//}
